// 诉状
Vue.component('ht-device', {
    name:'ht-device',
    template: `
        <el-dialog title="" :visible.sync="show" v-cloak
            :destroy-on-close="true" width="50%" top="20px" @close="close"
            :append-to-body="true" z-index="9999999">
            <el-form label-width="120px" :data="itemData" size="mini">
                <el-form-item label="选择设备">
                    <el-select v-model="info.id" placeholder="请选择"  filterable @change="setPrice">
                        <el-option
                                v-for="(item,index) in device_list"
                                :key="index"
                                :label="item.title  + (item.item ?  '|'+item.item : '')"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="数量">
                    <el-input placeholder="数量" v-model="info.num"></el-input>
                </el-form-item>
                <el-form-item label="价格">
                    <el-input placeholder="价格" v-model="info.price" readonly></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input placeholder="备注" v-model="info.bz"></el-input>
                </el-form-item>
                <el-form-item label="">
                    <el-button type="primary" @click="doAdd">确定</el-button>
                    <el-button type="" @click="close">返回</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    `,
    props: {
        factoryId:{
            type:Number,
            default:0
        },
        itemData:{
            type:Object,
            default:()=>{
                return {}
            }
        },
        show:{
            type:Boolean,
            default:false
        },
    },
    data() {
        return {
            device_list:[],
            info:{}
        }
    },
    created() {
        console.log("this.itemData",this.itemData)
    },
    methods: {
        setPrice(e) {
            let index = this.device_list.findIndex(item=>item.id == e)
            let item = this.device_list[index]
            vs.$set(this.info,'price',item.price)
        },
        doAdd(){
            let index = this.device_list.findIndex(item=>item.id == this.info.id)
            let item = this.device_list[index]
            item.num = this.info.num
            item.price = this.info.price
            item.bz = this.info.bz
            this.$emit('success',item)
            this.$emit('update:show',false)
        },
        close(){
            this.$emit('update:show',false)
            console.log('conse111')
        },
        handleSizeChange: function (e) {
            this.limit = e
        },
        handleCurrentChange: function (e) {
            this.page = e
        },
        getDevice(id) {
            Api.request("/admin/parts.device/all",{
                search_key:{factory_id:id}
            }).then(res=>{
                this.device_list = res.data
                this.device_list.map(item=>{
                    item.id = item.id+""
                })
            })
        },
    },
    watch: {
        itemData(v) {
            if (v.id) {
                this.$set(this.info,"id",v.id)
                this.$set(this.info,"bz",v.bz)
                this.$set(this.info,"num",v.num)
                this.$set(this.info,"price",v.price)
            } else {
                this.info = {}
            }
        },
        factoryId(v) {
            if (v) {
                this.getDevice(v)
            }
        }
    }
})